package com.incar.chuxing.activity.certification;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.incar.chuxing.R;
import com.incar.chuxing.activity.MainActivity;
import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlContainer;
import com.incar.chuxing.application.MyApplication;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.bean.PersonalCardRemindBean;
import com.incar.chuxing.utils.AppManager;
import com.incar.chuxing.utils.Logger;
import com.incar.chuxing.utils.ToastUtils;
import com.incar.chuxing.widget.CustomDatePicker;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * 身份认证 没有带证件，设置提醒时间
 */
public class CertifyCardRemindActivity extends BaseActivity {

    private Context mContext = this;
    private String dateRemind;
    private TextView currentDate;
    private CustomDatePicker customDatePicker;
    private String currentTime, selectTime;

    @BindView(R.id.tv_sure)
    TextView tv_sure;
    @BindView(R.id.layout_remind)
    RelativeLayout layout_remind;
    @BindView(R.id.tv_remindtime)
    TextView tv_remindtime;

    @Override
    public int getLayoutId() {
        return R.layout.activity_userverifyremind;
    }

    @Override
    public void initView() {
        initDatePicker();

    }

    @OnClick({R.id.layout_remind, R.id.tv_sure, R.id.title_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                AppManager.getAppManager().finishActivity();
                break;
            case R.id.layout_remind:
                customDatePicker.show(tv_remindtime.getText().toString());
                break;
            case R.id.tv_sure:
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:00");
                String selectTime = tv_remindtime.getText().toString();
                Date date = null;
                try {
                    date = format.parse(selectTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }


                selectTime = selectTime.replace("-", "").replace(":", "").replace(" ", "");
                currentTime = currentTime.replace("-", "").replace(":", "").replace(" ", "");
                long sTime = Long.valueOf(selectTime);
                long cTime = Long.valueOf(currentTime);
                if (sTime < cTime) {
                    ToastUtils.makeText(mContext, "选择日期必须大于当前时间", 1, ToastUtils.LENGTH_SHORT).show();
                } else {
                    setRemind(date.getTime() + "");
                }
                break;
        }
    }

    private void initDatePicker() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:00", Locale.CHINA);
        currentTime = sdf.format(new Date());
        tv_remindtime.setText(currentTime);

        customDatePicker = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                tv_remindtime.setText(time);
            }
        }, "2018-1-1 00:00", currentTime); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker.showSpecificTime(true); // 显示时和分
        customDatePicker.setIsLoop(true); // 允许循环滚动
    }

    private void setRemind(final String time) {
        showLoadingDialog();
        ApiStore.getInstance().getApiService(UrlContainer.Request.BASE_GATE_URL).getCardRemind(((MyApplication) getApplication()).getRequestFieldMap(), time).enqueue(new Callback<PersonalCardRemindBean>() {
            @Override
            public void onResponse(Call<PersonalCardRemindBean> call, Response<PersonalCardRemindBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    dismissLoadingDialog();
                    if (response.body().getStatus() == 200) {
                        ToastUtils.makeText(mContext, "设置成功", 0, ToastUtils.LENGTH_LONG).show();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Intent intent = new Intent(mContext, MainActivity.class);
                                startActivity(intent);
                                AppManager.getAppManager().finishActivity();
                            }
                        }, 1500);

                    } else {
                        ToastUtils.makeText(mContext, response.body().getMessage(), ToastUtils.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<PersonalCardRemindBean> call, Throwable t) {
                dismissLoadingDialog();
                ToastUtils.makeText(mContext, getResources().getString(R.string.badNetwork), ToastUtils.LENGTH_SHORT).show();
                Logger.d("neterror", "004" + t.toString());
            }
        });
    }

    @Override
    public void onBackPressed() {
        AppManager.getAppManager().finishActivity();
    }
}
